package com.concurrency.example.cancle;

import java.util.ArrayList;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * interrupt()中断线程
 * isInterrupted()返回中断标识
 * java没有强制中断线程的方法，interrupt()方法修改中断标识，由可能被中断的程序调用
 * @Auther: xuwei
 * @Date: 2018/12/10 09:41
 */

public class InterruptedTest {
    public static void main(String[] args) throws InterruptedException{
        /* NEW  isInterrupted=false 说明线程中断标识被清空，不起作用*/
//        Thread thread = new Thread(() -> {});
//        System.out.println(thread.getState());
//        Thread.sleep(1000);
//        thread.interrupt();
//        System.out.println(thread.isInterrupted());


        /* TERMINATED  isInterrupted=false 说明线程中断标识被清空，不起作用*/
//        Thread thread = new Thread(() -> {});
//        thread.start();
//        thread.join();
//        System.out.println(thread.getState());
//        Thread.sleep(1000);
//        thread.interrupt();
//        System.out.println(thread.isInterrupted());

        /*线程处于RUNNABLE状态，中断操作只是修改中断标识为true,对线程运行状态不做改变*/
//        Thread thread = new Thread(() -> {
//            while (true){
//            }
//        });
//        thread.start();
//        System.out.println(thread.getState());
//        thread.interrupt();
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println(thread.isInterrupted());
//        System.out.println(thread.getState());

        /*
        *线程处于WAITING状态，isInterrupted=false 说明线程中断标识被清空，不起作用
        * wait()会抛出一个检查型异常，如果线程中断会执行catch里的代码。
        */
//        Thread thread = new Thread(new Task());
//        thread.start();
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println(thread.getState());
//        thread.interrupt();
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println(thread.isInterrupted());
//        System.out.println(thread.getState());


        /*
           BLOCKED, 线程中断后中断标识置为true
         */
//        Thread thread1 = new Thread(InterruptedTest::doSomething);
//        thread1.start();
//        Thread thread2 = new Thread(InterruptedTest::doSomething);
//        thread2.start();
//
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println(thread2.getState());
//        thread2.interrupt();
//        TimeUnit.SECONDS.sleep(1);
//        System.out.println(thread2.getState());
//        System.out.println(thread2.isInterrupted());
//        thread2.interrupt();

        Task2 task1 = new Task2("34");
        Task2 task2 = new Task2("56");
        ArrayList<Task2> arrayList = new ArrayList<>();
        arrayList.add(task1);
        arrayList.add(task2);
        ExecutorService executorService = Executors.newFixedThreadPool(1);
        executorService.invokeAll(arrayList);
        System.out.println();
    }

    /**
     * 静态方法上的锁是类锁
     */
    private synchronized static void doSomething() {
        while (true) {

        }
    }

    static class Task2 implements Callable<String> {
        private String num;

        public Task2(String num) {
            this.num = num;
        }

        @Override
        public String call() throws InterruptedException{
            if ("34".equals(num)) {
                throw new InterruptedException();
            }
            System.out.println(num);
            return "";
        }
    }
    static class Task implements Runnable {
        @Override
        public void run() {
            synchronized (this) {
                try{
                    wait();
                }catch(InterruptedException e){
                    System.out.println("当线程被中断后执行的操作");
                }
            }
        }
    }
}
